package com.hndzyu.yd.customtask;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.CircleUploadBean;
import com.hndzyu.yd.bean.ScreenTaskBean;
import com.hndzyu.yd.bean.SubCard;
import com.hndzyu.yd.bean.SubInfo;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.utils.Util;
import com.hndzyu.yd.widget.CheckPopupWindow;
import com.hndzyu.yd.widget.MyGridView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//查看任务截图详情
public class PictureUploadDetailsActivity extends AppCompatActivity {

    @BindView(R.id.iv_chakan)
    ImageView ivChakan;
    @BindView(R.id.iv_isvip)
    ImageView iv_isvip;
    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.gridView)
    MyGridView gridView;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.ll_jinxing)
    LinearLayout llJinxing;
    @BindView(R.id.ll_top)
    LinearLayout ll_top;

    private int isshow = 1;  //1显示  0隐藏
    private int taskid = 0;
    private int checkid = 0;
    private ScreenTaskBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_upload_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorAccent).init();
        isshow = getIntent().getIntExtra("isshow", 1);
        if (isshow == 1) {
            bean = (ScreenTaskBean) getIntent().getExtras().getSerializable("item");
            taskid = bean.getData().getTaskid();
            tvText.setText(bean.getData().getTname());
            tvMoney.setText(bean.getJin() + "元");
            if (bean.getNum() != -1) {
                tvNum.setText("剩余" + bean.getNum() + "份");

            }
            if (bean.getData().getRuntime() != 0) {
                tvDay.setText("到期时间：" + Util.stampToDate(bean.getData().getRuntime() + ""));
            } else {
                tvDay.setVisibility(View.GONE);
            }
            if (bean.getData().getIsvip() == 1) {
                iv_isvip.setVisibility(View.VISIBLE);
            } else {
                iv_isvip.setVisibility(View.GONE);
            }
        } else {
            checkid = getIntent().getIntExtra("checkid", 0);
        }
        initData();
    }

    private void initData() {
        topTitle.setText("参与活动");
        if (isshow == 1) {
            getSubinfo(UserBean.uid, taskid);
        } else {
            if (isshow == 2) {
                getCircleinfo(checkid);
            } else {
                getCheckinfo(checkid);
            }
            ll_top.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.iv_chakan, R.id.top_back, R.id.tv_quxiao, R.id.tv_tongguo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_chakan:
                ivChakan.setVisibility(View.GONE);
                break;
            case R.id.top_back:
                finish();
                break;
            case R.id.tv_quxiao:
                //审核不通过
                //http://demo.itqzw.com/index.php/index/index/checkSubTask?checkid=1&status=2&scon=审核留言
                final CheckPopupWindow popupWindow = new CheckPopupWindow(this);
                popupWindow.showDown(tvMoney);
                popupWindow.setListener(new CheckPopupWindow.CheckListener() {
                    @Override
                    public void checkTrue(String reason) {
                        popupWindow.dismiss();
                        checkStatus(checkid, 3, reason);
                    }
                });
                break;
            case R.id.tv_tongguo:
                //审核通过
                checkStatus(checkid, 2, "");
                break;
            default:
                break;
        }
    }

    private void checkStatus(final int checkid, int status, String scon) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.checkSubTask + "?checkid=" + checkid + "&status=" + status + "&scon=" + scon,
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        if (!"1".equals(json) && !"2".equals(json)) {
                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(json);
                                if (jsonObject.has("status")) {
                                    Toast.makeText(PictureUploadDetailsActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                    getCheckinfo(checkid);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(PictureUploadDetailsActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void getSubinfo(int uid, int taskid) {
        if (bean.getData().getRuntime() != 0) {
            OkHttpDownloadJsonUtil.downloadJson(this, Path.getSubInfo + "?taskid=" + taskid + "&uid=" + uid,
                    new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                        @Override
                        public void onsendJson(String json) {
                            if (!"1".equals(json) && !"2".equals(json)) {
                                Gson gson = new Gson();
                                SubInfo bean = gson.fromJson(json, SubInfo.class);
                                tvContent.setText(bean.getData().getScontent());
                                List<String> url = Arrays.asList(bean.getData().getSimg().split(","));
                                gridView.setAdapter(new ImageAdapter(PictureUploadDetailsActivity.this, url));
                            }
                        }
                    });
        } else {
            OkHttpDownloadJsonUtil.downloadJson(this, Path.CARDPUSHDeTAIL(uid, taskid),
                    new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                        @Override
                        public void onsendJson(String json) {
                            if (!"1".equals(json) && !"2".equals(json)) {
                                Gson gson = new Gson();
                                SubCard bean = gson.fromJson(json, SubCard.class);
                                tvContent.setText(bean.getData().getScontent());
                                List<String> url = Arrays.asList(bean.getData().getContent().split(","));
                                gridView.setAdapter(new ImageAdapter(PictureUploadDetailsActivity.this, url));
                            }
                        }
                    });
        }
    }
    //发圈提交详情
    private void getCircleinfo(int checkid) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.showFq(UserBean.uid,checkid),
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try{
                            CircleUploadBean bean = new Gson().fromJson(json, CircleUploadBean.class);
                            tvContent.setText(bean.getData().getContent());
                            List<String> url = Arrays.asList(bean.getData().getImages().split(","));
                            gridView.setAdapter(new ImageAdapter(PictureUploadDetailsActivity.this, url));
                                switch (bean.getData().getStatus()) {
                                    case 3:
                                        tvStatus.setVisibility(View.VISIBLE);
                                        tvStatus.setText(bean.getData().getMoney());
                                        break;
                                    case -1:
                                        tvStatus.setVisibility(View.VISIBLE);
                                        tvStatus.setText(bean.getData().getScon());
                                        break;
                                   default:
                                       break;
                                }
                            }catch (Exception e){}
                        }
                    }
                });
    }

    private void getCheckinfo(int checkid) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.checkStatus + "?checkid=" + checkid,
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        if (!"1".equals(json) && !"2".equals(json)) {
                            Gson gson = new Gson();
                            SubInfo bean = gson.fromJson(json, SubInfo.class);
                            tvContent.setText(bean.getData().getScontent());
                            List<String> url = Arrays.asList(bean.getData().getSimg().split(","));
                            gridView.setAdapter(new ImageAdapter(PictureUploadDetailsActivity.this, url));
                            switch (bean.getData().getStatus()) {
                                case 0:
                                    llJinxing.setVisibility(View.GONE);
                                    tvStatus.setVisibility(View.VISIBLE);
                                    tvStatus.setText("用户已领取");
                                    break;
                                case 1:
                                    llJinxing.setVisibility(View.VISIBLE);
                                    tvStatus.setVisibility(View.GONE);
                                    break;
                                case 2:
                                    llJinxing.setVisibility(View.GONE);
                                    tvStatus.setVisibility(View.VISIBLE);
                                    tvStatus.setText("审核通过");
                                    break;
                                case 3:
                                    llJinxing.setVisibility(View.GONE);
                                    tvStatus.setVisibility(View.VISIBLE);
                                    tvStatus.setText("审核未通过");
                                    break;
                                case 4:
                                    llJinxing.setVisibility(View.GONE);
                                    tvStatus.setVisibility(View.VISIBLE);
                                    tvStatus.setText("任务超时");
                                    break;
                            }
                        }
                    }
                });
    }

    class ImageAdapter extends BaseAdapter {
        private List<String> datas;
        private Context mContext;

        public ImageAdapter(Context mContext, List<String> datas) {
            this.mContext = mContext;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_source_image, null);
                vh.ivImage = (ImageView) view.findViewById(R.id.iv_img);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            Glide.with(mContext).load(Path.BASE_URL + datas.get(i)).into(vh.ivImage);
            vh.ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ivChakan.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(Path.BASE_URL + datas.get(i)).into(ivChakan);
                }
            });
            return view;
        }

        class ViewHolder {
            public ImageView ivImage;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
